package cn.dinkevin.xui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.dinkevin.xui.R;

/**
 * 对话框工具辅助类
 * @author ：程序员小冰
 * @新浪微博 ：http://weibo.com/mcxiaobing
 * @GitHub: https://github.com/QQ986945193
 * @CSDN博客: http://blog.csdn.net/qq_21376985
 */
public class DialogHelper {

    /**
     * 展示自定义的对话框
     * @param context
     * @param isCancelable false -> dialog 弹出后会点击屏幕或物理返回键，dialog不消失
     * @param canceledOnTouchOutside false -> dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
     * @param customView
     * @return
     */
    public static Dialog showCustomDialog(Context context,View customView, boolean isCancelable,boolean canceledOnTouchOutside,int gravity){

        Dialog confirmDialog = new Dialog(context, R.style.transparent_dialog);    // 创建自定义样式dialog
        confirmDialog.setContentView(customView);
        confirmDialog.setCancelable(isCancelable);
        confirmDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);

        Window window = confirmDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(gravity);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        confirmDialog.show();
        return confirmDialog;
    }

    /**
     * 显示等待 Dialog
     * @param context  上下文
     * @param msg  显示内容
     * @param isCancelable 是否可以点击取消
     * @return
     */
    public static Dialog showWaitDialog(Context context, String msg,boolean isCancelable) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.xui_thrid_login_dialog_loading, null);             // 得到加载view
        RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.dialog_view);            // 加载布局

        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img_tip_icon);
        TextView tipTextView = (TextView) v.findViewById(R.id.txt_tip_content);   // 提示文字
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_animation);
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context, R.style.WhiteDialogStyle);    // 创建自定义样式dialog
        loadingDialog.setContentView(layout);
        loadingDialog.setCancelable(isCancelable);
        loadingDialog.setCanceledOnTouchOutside(false);

        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        loadingDialog.show();
        return loadingDialog;
    }

    /**
     * 关闭dialog
     * http://blog.csdn.net/qq_21376985
     * @param dialog
     */
    public static void closeDialog(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.cancel();
            dialog.dismiss();
        }
    }
}
